package com.yu.crm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.crm.domain.pojo.CrmUser;
import com.yu.crm.service.CrmUserService;
import com.yxzz.common.BaseController;
import com.yxzz.common.helper.valid.ValidatorHelpers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author donnie-bin
 * @since 2019-06-10
 */

@Api(tags = "用户管理")
@RestController
@RequestMapping("/crm-user")
public class CrmUserController implements BaseController<CrmUser> {

    @Autowired
    CrmUserService crmUserService;

    /**
     * 通过角色id查询角色对应的所有用户
     * @return
     */
    @GetMapping(value = "/info-by-token")
    @ApiOperation("通过token获取用户详细信息")
    public CrmUser infoByUserId(){
        return crmUserService.infoByUserId();
    }

    @ApiOperation(value = "新增或更新")
    @Override
    @PostMapping(value = "/save-or-update")
    public void saveOrUpdate(@Valid CrmUser entity, BindingResult bindingResult) {
        ValidatorHelpers.validateParams(bindingResult);
        crmUserService.saveOrUpdate(entity);
    }

    /**
     * 通过userId修改人脸特征值
     * @param crmUser
     */
    @PostMapping(value = "/update-by-userId")
    public void updateByUserId(@RequestBody CrmUser crmUser){
        ValidatorHelpers.notEmpty(crmUser.getUserId(), "userId不能为空");
        ValidatorHelpers.notEmpty(crmUser.getUserFaceUrl(), "用户人脸照片不能为空");
        crmUserService.updateByUserId(crmUser);
    }

    @ApiOperation(value = "删除")
    @Override
    @DeleteMapping(value = "/remove-by-id/{id}")
    public void removeById(@PathVariable Long id) {
        crmUserService.removeById(id);
    }

    @ApiOperation(value = "详情查询")
    @Override
    @GetMapping(value = "/get-by-id/{id}")
    public CrmUser getById(@PathVariable Long id) {
        return crmUserService.getById(id);
    }

    @ApiOperation(value = "通过userId查询详情")
    @GetMapping(value = "/get-by-userId")
    public CrmUser getByUserId(String userId) {
        return crmUserService.getByUserId(userId);
    }

    @ApiOperation(value = "分页列表查询")
    @Override
    @GetMapping(value = "/list")
    public IPage<CrmUser> list(Page<CrmUser> page, CrmUser queryInfo) {
        return crmUserService.list(page, queryInfo);
    }

    @ApiOperation(value = "修改用户密码")
    @PostMapping(value = "/update-pwd")
    public Boolean updatePwd(@RequestBody CrmUser entity) {
        ValidatorHelpers.notEmpty(entity.getUserAccountId(), "账户不能为空");
        return crmUserService.updatePwd(entity);
    }


    /**
     * 通过用户名和密码登录（自己后台登录）
     * #@param username 用户名
     * #@param pwd 密码
     * @param map 用户名和密码
     */
    @PostMapping(value = "/login")
    public Map<String, Object> login(@RequestBody HashMap<String,Object> map){
        ValidatorHelpers.notEmpty(map.get("username"), "用户名不能为空");
        ValidatorHelpers.notEmpty(map.get("pwd"), "密码不能为空");
        return crmUserService.login(map);
    }
}

